package pl.com.kyhu.dietex3000.GUI.components;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.wb.swt.SWTResourceManager;

import pl.com.kyhu.dietex3000.database.beans.Meal;
import pl.com.kyhu.dietex3000.database.beans.MealCategory;
import pl.com.kyhu.dietex3000.database.management.DataManagement;
import pl.com.kyhu.dietex3000.utils.Constants;

public class MealTree extends Composite {

	Tree tree;

	private Map<TreeItem, Meal> treeMeals;
	private Text recipeLabel = null;

	public MealTree(Composite parent, int style) {
		super(parent, SWT.BORDER);

		tree = new Tree(this, SWT.CHECK | SWT.FULL_SELECTION | SWT.MULTI);
		setLayout(new FillLayout(SWT.HORIZONTAL));
		refreshData();

		tree.addListener(SWT.MouseDown, new Listener() {
			public void handleEvent(Event event) {
				Point point = new Point(event.x, event.y);
				Meal meal = treeMeals.get(tree.getItem(point));
				if (meal != null) recipeLabel.setText(meal.toString());
			}
		});

		tree.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				if (event.detail == SWT.CHECK) {
					TreeItem item = (TreeItem) event.item;
					boolean checked = item.getChecked();
					for (TreeItem subItem : item.getItems()) {
						subItem.setChecked(checked);
						for (TreeItem subSubItem : subItem.getItems()) {
							subSubItem.setChecked(checked);
						}
					}
				}
			}
		});

		Menu popupMenu = new Menu(this);
		tree.setMenu(popupMenu);

		MenuItem refreshMenuItem = new MenuItem(popupMenu, SWT.NONE);
		refreshMenuItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				refreshData();
			}
		});
		refreshMenuItem.setText("Od\u015Bwie\u017C");

		tree.pack();

	}

	public void refreshData() {
		tree.removeAll();
		treeMeals = new HashMap<TreeItem, Meal>();
		tree.setLayout(new FillLayout(SWT.HORIZONTAL));
		List<MealCategory> categories = DataManagement.getMealSubCategories(Constants.mealCategoryNode);

		boolean emptyItem;
		boolean emptySubItem;
		for (MealCategory category : categories) {
			emptyItem = true;
			TreeItem item = new TreeItem(tree, SWT.CHECK);
			item.setChecked(true);
			item.setText(category.getName());
			item.setFont(SWTResourceManager.getFont("Microsoft Sans Serif", 8, SWT.BOLD));
			item.setBackground(SWTResourceManager.getColor(232, 232, 232));

			for (MealCategory subCategory : DataManagement.getMealSubCategories(category)) {
				emptySubItem = true;

				TreeItem subItem = new TreeItem(item, SWT.CHECK);
				subItem.setChecked(true);
				subItem.setText(subCategory.getName());
				subItem.setFont(SWTResourceManager.getFont("Microsoft Sans Serif", 8, SWT.BOLD));
				subItem.setBackground(SWTResourceManager.getColor(239, 239, 239));

				for (Meal meal : DataManagement.getMealsWithCategory(subCategory)) {
					emptySubItem = false;
					TreeItem mealItem = new TreeItem(subItem, SWT.CHECK);
					mealItem.setChecked(true);
					mealItem.setText(meal.getName());
					treeMeals.put(mealItem, meal);
				}

				if (emptySubItem) subItem.dispose();
				else
					emptyItem = false;

			}
			for (Meal meal : DataManagement.getMealsWithCategory(category)) {
				emptyItem = false;
				TreeItem mealItem = new TreeItem(item, SWT.CHECK);
				mealItem.setChecked(true);
				mealItem.setText(meal.getName());
				treeMeals.put(mealItem, meal);
			}

			if (emptyItem) item.dispose();
		}

	}

	public void setRecipeText(Text recipeLabel) {
		this.recipeLabel = recipeLabel;
	}

	public ArrayList<Meal> getCheckedMeals() {
		ArrayList<Meal> checkedMeals = new ArrayList<Meal>();

		for (TreeItem item : tree.getItems()) {
			for (TreeItem subItem : item.getItems()) {
				if (subItem.getItemCount() > 0) {
					for (TreeItem mealItem : subItem.getItems()) {
						if (mealItem.getChecked()) {
							checkedMeals.add(treeMeals.get(mealItem));
						}
					}
				} else {
					if (subItem.getChecked()) {
						checkedMeals.add(treeMeals.get(subItem));
					}
				}
			}
		}

		return checkedMeals;
	}

}
